package org.microser.gataway.zuul.filter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StreamUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
//import com.syher.zuul.core.zuul.ContantValue;

/**
 * 登录返回token拦截
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-11-24 11:01
 **/
@Slf4j
public class AbstractZuulFilter extends ZuulFilter {

    protected RequestContext context;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public boolean shouldFilter() {
        return RequestContext.getCurrentContext().getRequest().getRequestURI().equals("/api/auth/login");
    }

    @Override
    public Object run() {
        context = RequestContext.getCurrentContext();
        System.out.println("登录完成拦截token");

        context.setSendZuulResponse(false);
        context.getResponse().setContentType("text/html;charset=UTF-8");
        context.setResponseStatusCode(200);


        InputStream stream = context.getResponseDataStream();
        String body = null;
        try {
            body = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
//            context.setResponseDataStream(new ByteArrayInputStream(body.getBytes()));
            System.out.println(body);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //accessToken
        JSONObject result = JSONObject.parseObject(body);
        JSONObject data = result.getJSONObject("data");
//        System.out.println("data = "+data);
        if(data==null){
            //登录失败，直接放回
            context.setResponseBody(result.toJSONString());
            return null;
        }
        Object accessToken = data.get("accessToken");
        if(accessToken==null){
            //登录失败，直接放回
            context.setResponseBody(result.toJSONString());
            return null;
        }
        System.out.println("accessToken = "+accessToken);

        redisTemplate.opsForValue().set(accessToken.toString(),accessToken.toString());
        redisTemplate.expire(accessToken.toString(), 30 * 60 * 1000, TimeUnit.SECONDS);

        //生成uuid 对应 token
//        String uuid = UUID.randomUUID().toString().replace("-", "");
//        redisTemplate.opsForValue().set(uuid,accessToken.toString());
//        redisTemplate.expire(uuid, 30 * 60 * 1000, TimeUnit.SECONDS);
//        data.put("accessToken",uuid);

//        Map<String,Object> map = new HashMap();
//        map.put("status",result.get("status"));
//        map.put("message",result.get("message"));
//        map.put("data",uuid);//data
//        context.setResponseBody(JSONObject.toJSONString(map));

        context.setResponseBody(result.toJSONString());
        return null;
    }


    @Override
    public String filterType() {
        return "post";
    }

    @Override
    public int filterOrder() {
        return 0;
    }
}